// 用户角色列表
Ext.ns('AppRoleView');
Ext.define('AppRoleView', {
	extend : 'Ext.panel.Panel',
	id : 'AppRoleView',
	title : '角色列表',
	iconCls : 'menu-role',
	layout : 'border',

	initComponent : function() {
		// 初始化搜索条件Panel
		this.searchPanel = new KE.SearchPanel({
					region : 'north',
					layout : {
						type : 'hbox',
						align : 'middle'
					},
					defaults : {
						xtype : 'label',
						style : 'margin-left:10px;'
					},
					items : [{
								text : '角色名称'
							}, {
								xtype : 'textfield',
								name : 'Q_roleName_S_LK'
							}, {
								text : '角色描述'
							}, {
								xtype : 'textfield',
								name : 'Q_roleDesc_S_LK'
							}, {
								xtype : 'button',
								text : '查询',
								scope : this,
								iconCls : 'btn-search',
								handler : this.search
							}, {
								xtype : 'button',
								text : '清空',
								scope : this,
								iconCls : 'reset',
								handler : this.reset
							}]
				});

		// Grid的工具栏
		this.toolbar = new Ext.Toolbar({
					items : [{
								iconCls : 'btn-add',
								text : '添加角色',
								handler : function() {
									new AppRoleForm().show();
								}
							}, '-', {
								iconCls : 'btn-del',
								text : '删除角色',
								scope : this,
								handler : this.deleteRole
							}]
				});
//		if (isGranted('_AppRoleAdd')) {
//			this.toolbar.add(new Ext.Button({
//						iconCls : 'btn-add',
//						text : '添加角色',
//						handler : function() {
//							new AppRoleForm().show();
//						}
//					}));
//		}
//		this.toolbar.add('-');
//		if (isGranted('_AppRoleDel')) {
//			this.toolbar.add(new Ext.Button({
//						iconCls : 'btn-del',
//						text : '删除角色',
//						scope : this,
//						handler : this.deleteRole
//					}));
//		}
		// Grid表格
		this.gridPanel = new KE.GridPanel({
			region : 'center',
			tbar : this.toolbar,
			id : 'AppRoleGrid',
			trackOver : true,
			disableSelection : false,
			url : __ctxPath + '/system/listAppRole.do',
			fields : [{
						name : 'roleId',
						type : 'int'
					}, 'roleName', 'roleDesc', {
						name : 'status',
						type : 'int'
					}, 'isDefaultIn'],
			columns : [{
						header : 'roleId',
						dataIndex : 'roleId',
						hidden : true
					}, {
						header : "状态",
						dataIndex : 'status',
						width : 30,
						renderer : function(value) {
							var str = '';
							if (value == '1') {// 激活用户
								str += '<img title="激活" src="'
										+ __ctxPath
										+ '/images/flag/customer/effective.png"/>'
							} else {// 禁用用户
								str += '<img title="禁用" src="'
										+ __ctxPath
										+ '/images/flag/customer/invalid.png"/>'
							}
							return str;
						}
					}, {
						header : "角色名称",
						dataIndex : 'roleName',
						width : 200
					}, {
						header : "角色描述",
						dataIndex : 'roleDesc',
						width : 400
					}, {
						header : '管理',
						dataIndex : 'roleId',
						width : 80,
						renderer : function(value, metadata, record, rowIndex,
								colIndex) {
							var editId = record.data.roleId;
							var roleName = record.data.roleName;
							var isDefaultIn = record.data.isDefaultIn;
							var str = '';
							if (editId != 1) {
								if (isDefaultIn == '0') {
									if (isGranted('_AppRoleDel'))
										str = '<button title="删除" value=" " class="btn-del" onclick="AppRoleView.remove('
												+ editId
												+ ')"></button>&nbsp;&nbsp;&nbsp;&nbsp;';
									if (isGranted('_AppRoleEdit'))
										str += '<button title="编辑" value=" " class="btn-edit" onclick="AppRoleView.edit('
												+ editId
												+ ')"></button>&nbsp;&nbsp;&nbsp;&nbsp;';
									if (isGranted('_AppRoleGrant'))
										str += '<button title="授权" value=" " class="btn-grant" onclick="AppRoleView.grant('
												+ editId
												+ ',\''
												+ roleName
												+ '\')">&nbsp;</button>&nbsp;&nbsp;&nbsp;&nbsp;';

								} else {
									str = '<button title="复制" value=" " class="btn-copyrole" onclick="AppRoleView.copy('
											+ editId + ')"></button>';
								}
							}
							return str;
						}
					}]
		});

		this.items = [this.searchPanel, this.gridPanel];
		this.callParent();
		this.gridPanel.on('rowdblclick', this.rowdblclick, this);

	},
	// 行双击
	rowdblclick : function(grid, rec) {
		if (rec.data.isDefaultIn == '0' && rec.data.roleId != -1) {
			AppRoleView.edit(rec.data.roleId);
		}
	},
	// 重置查询表单
	reset : function() {
		this.searchPanel.getForm().reset();
	},
	// 按条件搜索
	search : function() {
		$search({
					searchPanel : this.searchPanel,
					gridPanel : this.gridPanel
				});
	},
	// 删除角色
	deleteRole : function() {
		var grid = Ext.getCmp("AppRoleGrid");
		var selectRecords = grid.getSelection();

		if (selectRecords.length == 0) {
			Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
			return;
		}
		var ids = Array();
		var idsN = '';
		for (var i = 0; i < selectRecords.length; i++) {
			if (selectRecords[i].data.isDefaultIn == '0'
					&& selectRecords[i].data.roleId != -1) {
				ids.push(selectRecords[i].data.roleId);
			} else {
				idsN += selectRecords[i].data.roleName + ',';
			}
		}
		if (idsN == '') {
			AppRoleView.remove(ids);
		} else {
			Ext.ux.Toast.msg("信息", idsN + "不能被删除！");
		}

	}

});

/**
 * 删除单个记录
 */
AppRoleView.remove = function(id) {
	var grid = Ext.getCmp("AppRoleGrid");
	Ext.Msg.confirm('信息确认', '您确认要删除该记录吗？', function(btn) {
				if (btn == 'yes') {
					Ext.Ajax.request({
								url : __ctxPath + '/system/multiDelAppRole.do',
								params : {
									ids : id
								},
								method : 'post',
								success : function() {
									Ext.ux.Toast.msg("信息", "成功删除所选记录！");
									grid.getStore().reload({
												params : {
													start : 0,
													limit : 25
												}
											});
								}
							});
				}
			});
};

/**
 * 
 */
AppRoleView.edit = function(id) {
	new AppRoleForm({
				roleId : id,
				isCopy : 0
			}).show();// 0代表不是复制
};

AppRoleView.grant = function(id, roleName) {
	new RoleGrantRightView({
				roleId : id,
				roleName : roleName
			}).show();
};

AppRoleView.copy = function(id) {
	new AppRoleForm({
				roleId : id,
				isCopy : 1
			}).show();// 1代表是复制
}
